package com.sinothk.gpserver.modules.texasPoker.helper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 编写 Liang Y.T.
 * 时间 2021/6/29 15:02
 */
public class ListUtils {
    /**
     * 将一个List均分成n个list,主要通过偏移量来实现的
     *
     * @param source 源集合
     * @param limit 最大值
     * @return
     */
    public static <T> ArrayList<ArrayList<T>> averageAssign(ArrayList<T> source, int limit) {
        if (null == source || source.isEmpty()) {
            return new ArrayList<>();
        }
        ArrayList<ArrayList<T>> result = new ArrayList<>();
        int listCount = (source.size() - 1) / limit + 1;
        int remaider = source.size() % listCount; // (先计算出余数)
        int number = source.size() / listCount; // 然后是商
        int offset = 0;// 偏移量
        for (int i = 0; i < listCount; i++) {
            ArrayList<T> value;
            if (remaider > 0) {
                value = new ArrayList<>(source.subList(i * number + offset, (i + 1) * number + offset + 1));
                remaider--;
                offset++;
            } else {
                value = new ArrayList<>(source.subList(i * number + offset, (i + 1) * number + offset));
            }
            result.add(value);
        }
        return result;
    }

    /**
     * 使用迭代器可，正确无误的删除
     *
     * @param list
     * @param element
     */
    public static void iteratorRemove(List list, Object element) {
        // 注意！！！这里时Iterator.remove()!!!而不是list.remove()!!!
        list.removeIf(cur -> cur.equals(element));
    }
}
